package com.example.seataOrder.service;

import com.example.seataOrder.common.enums.RspStatusEnum;
import com.example.seataOrder.common.response.ObjectResponse;
import com.example.seataOrder.entity.Order;
import com.example.seataOrder.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    private final OrderMapper orderMapper;

    public OrderService(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    /**
     * 创建订单
     *
     * @param userId        用户ID
     * @param commodityCode 产品编号
     * @param count         商品数量
     * @param orderMoney    订单总金额
     */
    public ObjectResponse<Object> createOrder(String userId, String commodityCode, Integer count, Double orderMoney) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        Order order = new Order()
                .setOrderNo(UUID.randomUUID().toString().toLowerCase().replace("-", ""))
                .setCommodityCode(commodityCode)
                .setUserId(userId)
                .setCount(count)
                .setAmount(orderMoney);
        int result = orderMapper.insert(order);
        if (result > 0) {
            response.setStatus(RspStatusEnum.SUCCESS.getCode());
            response.setMessage(RspStatusEnum.SUCCESS.getMessage());
            return response;
        }
        response.setStatus(RspStatusEnum.FAIL.getCode());
        response.setMessage(RspStatusEnum.FAIL.getMessage());
        return response;
    }
}
